//
//  UIImage+Radius.m
//  MarketingApp
//
//  Created by 李叶夫 on 2018/4/13.
//  Copyright © 2018年 李叶夫. All rights reserved.
//

#import "UIImage+Radius.h"

@implementation UIImage (Radius)

- (UIImage *)lyf_setCornerImageWithText:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont fillColor:(UIColor *)fillColor opaque:(BOOL)opaque size:(CGSize)size cornerRadius:(CGFloat)cornerRadius
{
    //获取文字size
    NSDictionary *attributes = @{NSFontAttributeName:textFont,NSForegroundColorAttributeName:textColor};
    CGSize textSize = [text sizeWithAttributes:attributes];
    CGPoint textDrawPoint = CGPointMake((size.width-textSize.width)/2, (size.height-textSize.height)/2);
    //开起上下文
    UIGraphicsBeginImageContextWithOptions(size, opaque, 0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //绘制圆角矩形的贝塞尔曲线
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:cornerRadius];
    //绘制填充色
    CGContextSetFillColorWithColor(ctx, fillColor.CGColor);
    [path fill];
    //绘制文字
    [text drawAtPoint:textDrawPoint withAttributes:attributes];
    UIImage *res = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return res;
}

+ (UIImage *)lyf_setImageWithText:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont fillColor:(UIColor *)fillColor size:(CGSize)size
{
    //获取文字size
    NSDictionary *attributes = @{NSFontAttributeName:textFont,NSForegroundColorAttributeName:textColor};
    CGSize textSize = [text sizeWithAttributes:attributes];
    CGPoint textDrawPoint = CGPointMake((size.width-textSize.width)/2, (size.height-textSize.height)/2);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), fillColor.CGColor);
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, size.width, size.height)];
    [path fill];
    [text drawAtPoint:textDrawPoint withAttributes:attributes];
    UIImage *res = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return res;
}

- (void)lyf_setCornerImageWithText:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont fillColor:(UIColor *)fillColor opaque:(BOOL)opaque size:(CGSize)size cornerRadius:(CGFloat)cornerRadius completion:(void (^)(UIImage *))completion
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        UIImage *img = [self lyf_setCornerImageWithText:text textColor:textColor textFont:textFont fillColor:fillColor opaque:opaque size:size cornerRadius:cornerRadius];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                completion(img);
            }
        });
    });
}

- (UIImage *)lyf_setCornerImageWithSize:(CGSize)size fillColor:(UIColor *)fillColor opaque:(BOOL)opaque cornerRadius:(CGFloat)cornerRadius
{
    UIGraphicsBeginImageContextWithOptions(size, opaque, 0);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    if (opaque) {
        [fillColor setFill];
        UIRectFill(rect);
    }
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
    [path addClip];
    [self drawInRect:rect];
    UIImage *res = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return res;
}

- (UIImage *)lyf_setCornerImageWithSize:(CGSize)size cornerRadius:(CGFloat)cornerRadius
{
//    return [self lyf_setCornerImageWithSize:size fillColor:nil opaque:NO cornerRadius:cornerRadius];
    return [self lyf_setCornerImageWithSize:size fillColor:[UIColor whiteColor] opaque:YES cornerRadius:cornerRadius];
}

- (void)lyf_setCornerImageWithSize:(CGSize)size fillColor:(UIColor *)fillColor opaque:(BOOL)opaque cornerRadius:(CGFloat)cornerRadius completion:(void (^)(UIImage *))completion
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        UIImage *img = [self lyf_setCornerImageWithSize:size fillColor:fillColor opaque:opaque cornerRadius:cornerRadius];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                completion(img);
            }
        });
    });
}

+ (UIImage *)lyf_setCornerImageWithSize:(CGSize)size fillColor:(UIColor *)fillColor
{
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //绘制圆角矩形的贝塞尔曲线
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:size.height/2.0];
    CGContextSetFillColorWithColor(ctx, fillColor.CGColor);
    [path fill];
    CGContextFillRect(ctx, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

@implementation UIImageView (WebRadius)

- (void)lyf_setCornerImageWithUrl:(NSURL *)url placeholder:(UIImage *)placeholder cornerRadius:(CGFloat)cornerRadius
{
    if (placeholder) {
        placeholder = [placeholder lyf_setCornerImageWithSize:self.bounds.size cornerRadius:cornerRadius];
    }
    if (cornerRadius != 0.0) {
        NSString *cacheurlStr = [url.path stringByAppendingString:@"radiusCache"];
        UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:cacheurlStr];
        if (cacheImage) {
            self.image = cacheImage;
        } else {
            [self sd_setImageWithURL:url placeholderImage:placeholder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                if (!error) {
                    UIImage *radiusImage = [image lyf_setCornerImageWithSize:self.bounds.size cornerRadius:cornerRadius];
                    self.image = radiusImage;
                    [[SDImageCache sharedImageCache] storeImage:radiusImage forKey:cacheurlStr completion:nil];
                    [[SDImageCache sharedImageCache] removeImageForKey:url.path withCompletion:nil];
                }
            }];
        }
    } else {
        [self sd_setImageWithURL:url placeholderImage:placeholder completed:nil];
    }
    
}



@end


@implementation UIButton (Radius)

- (void)lyf_setBorderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth cornerRadius:(CGFloat)cornerRadius
{
    
}

@end

